package com.gqzm.iaas.utils;

import lombok.*;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;

import java.io.Serializable;

/**
 * @ClassName: {@link UserUtils}
 * @description: 用户信息工具类
 * @author: peter qin
 * @date: 2023 07 14
 * @version: 1.0
 **/
@Slf4j
@UtilityClass
public class UserUtils {

    //用户信息线程变量
    private static final ThreadLocal<User> userLocal = new ThreadLocal<>();

    @Getter
    @Setter
    @Builder
    @NoArgsConstructor
    @AllArgsConstructor
    public static class User implements Serializable {
        //用户编码
        private String userCode;
        //用户名称
        private String userName;
        //用户角色
        private String roleType;
    }


    /**
     * 设置当前线程用户信息
     *
     * @param user 用户信息
     */
    public static void setUser(User user) {
        userLocal.set(user);
    }


    /**
     * 获取当前线程用户信息
     *
     * @return {@link User}
     */
    public static User getUser() {
        return userLocal.get();
    }


    /**
     * 异常当前用户信息
     */
    public static void removeUser() {
        userLocal.remove();
    }


}
